package com.tgpms.web.earlierStage.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.entity.DocSortTemplate;
import com.tgpms.web.earlierStage.service.EarlierDocSortTempService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "EarlierDocSortTempController", tags = "前期 文档分类模板相关接口")
@RestController
@RequestMapping(value = "/ev/earlierDocSortTemp")
public class EarlierDocSortTempController {
	
	@Autowired
	private EarlierDocSortTempService earlierDocSortTempService;
	@Autowired
	private TokenService tokenService;

	@ApiOperation(value = "新增文档分类模板接口", notes = "新增文档分类模板接口", httpMethod = "POST")
	@PostMapping(value = "/addDocSortTemp")
	public Result addDocSortTemp(@RequestBody DocSortTemplate docSortTemp,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(docSortTemp.getTaskId())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|唯一键为空");
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			docSortTemp.setInsertUser(insertName);
			result = earlierDocSortTempService.addDocSortTemp(docSortTemp, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	
	@ApiOperation(value = "修改文档分类模板接口", notes = "修改文档分类模板接口", httpMethod = "PUT")
	@PutMapping(value = "/updateDocSortTemp")
	public Result updateDocSortTemp(@RequestBody DocSortTemplate docSortTemp,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(docSortTemp.getDstId())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			docSortTemp.setModifyUser(insertName);
			result = earlierDocSortTempService.updateDocSortTemp(docSortTemp, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	@ApiOperation(value = "删除文档分类模板接口", notes = "删除文档分类模板接口", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteDocSortTemp")
	public Result deleteDocSortTemp(@RequestParam(value = "dstId", required = true) String dstId,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(dstId)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			result = earlierDocSortTempService.deleteDocSortTemp(dstId, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}
	
}
